package com.dy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.entity.OrderVo;
import com.dy.entity.Sale;
import com.dy.service.IFeignOrderService;
import com.dy.service.ISaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/sale")
@Api(tags = "销售系统操作接口")
public class SaleController {
    @Autowired
    private ISaleService saleService;
    @Autowired
    private IFeignOrderService feignOrderService;

    @GetMapping("/selectAllSale/{pageNum}/{pageSize}")
    @ApiOperation("查询单页销售电影票")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面展示条数")
    })
    public Page<Sale> selectAllSale(@PathVariable("pageNum")Integer pageNum,
                                    @PathVariable("pageSize")Integer pageSize){
        Page<Sale> page = new Page<>(pageNum-1,pageSize);
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("sale_state",0l);
        return saleService.page(page,wrapper);
    }

    @PostMapping("/buy")
    @ApiOperation("下单")
    public String buy(OrderVo orderVo){
        Sale sale = new Sale();
        if (orderVo.getSaleNumber() < orderVo.getOrderNum()){
            return "余票不足";
        }
        sale.setCinameNum(orderVo.getSaleNumber()- orderVo.getOrderNum());
        sale.setSaleNum(orderVo.getOrderNum());
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("ciname_name",orderVo.getCinemaName());
        boolean update = saleService.update(sale,wrapper);
        if (!update){
            return "下单失败";
        }
        System.out.println(orderVo);
        if (feignOrderService.insertOrder(orderVo)){
            return "下单成功";
        }
        return "下单失败";
    }
}
